package com.chookss.home.search.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.Token;
import com.chookss.home.interview.InterviewDetailActivity;
import com.chookss.home.search.entity.SearchAllEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.Utils;
import com.chookss.view.MyImageSpan;

import java.util.List;

import io.reactivex.annotations.Nullable;

public class SearchTuwenAdapter extends BaseQuickAdapter<SearchAllEntity.ImageTextBean.ListBean, BaseViewHolderEx> {
    private Context context;
    private int width, height;
    private String key;

    public SearchTuwenAdapter(int layoutResId, Context context, @Nullable List<SearchAllEntity.ImageTextBean.ListBean> data) {
        super(layoutResId, data);
        this.context = context;
        width = (Utils.getScreenWidthPixel(context) - Utils.dip2px(context, 48)) / 3;
        height = width * 65 / 102;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, SearchAllEntity.ImageTextBean.ListBean item) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        RelativeLayout rlImgChoice = helper.getView(R.id.rlImgChoice);
        ImageView ivChoice = helper.getView(R.id.ivChoice);
        View line = helper.getView(R.id.line);
        TextView tvName = helper.getView(R.id.tvName);
        TextView tvDate = helper.getView(R.id.tvDate);
        LinearLayout llOneImg = helper.getView(R.id.llOneImg);
        LinearLayout llThreeImg = helper.getView(R.id.llThreeImg);
        ImageView ivImg = helper.getView(R.id.ivImg);
        ImageView ivPic1 = helper.getView(R.id.ivPic1);
        ImageView ivPic2 = helper.getView(R.id.ivPic2);
        ImageView ivPic3 = helper.getView(R.id.ivPic3);
        View placeholderView = helper.getView(R.id.placeholderView);
        View spaceView = helper.getView(R.id.spaceView);
        //两个view只能同时存在一个
        LinearLayout llContent = helper.getView(R.id.llContent);
        rlImgChoice.setVisibility(View.GONE);
        String title = item.getTitle().trim().replace("\r|\n", "");
        LinearLayout.LayoutParams llOneImgParams = (LinearLayout.LayoutParams) llOneImg.getLayoutParams();
        LinearLayout.LayoutParams llContentParams = (LinearLayout.LayoutParams) llContent.getLayoutParams();
        tvTitle.setText(title);
        tvName.setText(item.getCreate_employee_name());
        tvDate.setText(Utils.getAccurateDate(item.getCreate_time()));
        if (!Utils.isNull(item.getPictureUrls())) {
            llOneImg.setVisibility(View.VISIBLE);
            llThreeImg.setVisibility(View.GONE);
            llOneImgParams.height = height;
            llOneImgParams.width = width;
            llOneImg.setLayoutParams(llOneImgParams);
            llContentParams.height = height;
            placeholderView.setVisibility(View.VISIBLE);
            spaceView.setVisibility(View.GONE);
            llContent.setLayoutParams(llContentParams);
            GlideUtils.loadCustomRound(context, item.getPictureUrls(), ivImg, 4);
        } else {
            llOneImg.setVisibility(View.GONE);
            llThreeImg.setVisibility(View.GONE);
            placeholderView.setVisibility(View.GONE);
            spaceView.setVisibility(View.VISIBLE);
            llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            llContent.setLayoutParams(llContentParams);

        }

        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addressType = "";
                if (Urls.baseUrl.contains("dev_kmer")) {
                    addressType = "1";
                } else {
                    addressType = "0";
                }
                String url = Urls.tuwenDetail + "mainCode=" + item.getMain_code() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                Intent intent = new Intent(context, WebCommonActivity.class);
                intent.putExtra("url", url);
                intent.putExtra("title", "详情");
                intent.putExtra("isTuwen", 3);
                intent.putExtra("waterMark", "13");
                intent.putExtra("url", url);
                intent.putExtra("createEmployeeName", item.getCreate_employee_name());
                intent.putExtra("newsTitle", item.getTitle());
                intent.putExtra("resourceId", item.getMain_code());
                intent.putExtra("newsContent", "");
                if (Utils.isNull(item.getPictureUrls())) {
                    intent.putExtra("picUrl", "");
                } else {
                    intent.putExtra("picUrl", Utils.isNull(item.getPictureUrls()));
                }
                context.startActivity(intent);
            }
        });

    }

    public void setKey(String key) {
        this.key = key;
    }
}
